<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单编辑控制器
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 */

class pa_billEdit {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //订单修改
                    $this->alter();
                    break;

                case 3: //新增订单
                    $this->add();
                    break;

                case 4: //新增澳粒订单
                    $this->AL_add();
                    break;

                case 5: //澳粒订单修改
                    $this->AL_alter();
                    break;

                default:
                    backendError();
                    break;

            }
        } else {

            backendError();

        }

    }

    /**
     * 订单新增
     */
    private function add() {
        global $G_PGLOB;

        if (!isset($_REQUEST['pcustID']) || empty($_REQUEST['pcustID'])) {
            backendError();
        }

        //城市切换
        if(isset($_REQUEST['cityid']) && is_numeric($_REQUEST['cityid'])){
            $cityid = $_REQUEST['cityid'];
        }

        //订单操作
        if(isset($_REQUEST['operateType']) && is_numeric($_REQUEST['operateType'])){
            $G_PGLOB->smarty->assign('operateType', $_REQUEST['operateType']);
        }

        //属性
        if (!isset($_REQUEST['properties'])) {
            $properties = '';
        } else {
            $properties = $_REQUEST['properties'];
        }



        $billType = 1;  //默认正常订单

        $pcustID = $_REQUEST['pcustID'];

        

        //加载类文件
        quanli_require('module/base/customer/quanliCust.class.php');
        quanli_require('module/base/system/itemHandler.class.php');
        quanli_require('module/base/promotion/quanliCoupon.class.php');
        quanli_require('module/func/eai.func.php');

        //实例item类
        $itemHandler = new itemHandler();

        //实例会员类
        $quanliCust = new quanliCust();

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon();

        //实例化接口操作类
        $quanliEai = eai_crm();

        //查询会员信息
        $args_array = array(
            'pcustID' => $pcustID
        );
        $customer_array = $quanliCust->searchCustomerDetail( $args_array );
        if (!$customer_array) {
            backendError('会员信息没有找到');
        }

        //会员余额
        $result_array = $quanliEai->searchCustAccount($pcustID, G_CASH_ID);
        if($result_array['code'] == 200){
            $customer_array['cash'] = $result_array['data']['useableAccount'];
        } else {
            $customer_array['cash'] = 0;
        }

        //会员积分
        $result_array = $quanliEai->searchCustAccount($pcustID, G_POINTS_ID);
        if ($result_array['code'] == 200) {
            $customer_array['points'] = $result_array['data']['useableAccount'];
            $customer_array['points_price'] = round($result_array['data']['useableAccount'] / 100, 2);
        } else {
            $customer_array['points'] = 0;
            $customer_array['points_price'] = 0;
        }

        $buy_log_array = $quanliCust->getBuyLog( $pcustID );
        $buy_log_array['invoiceID'] = null;
        $buy_log_array['payTypeID'] = null;

        //查询城市信息
        $city_array = $G_PGLOB->getCache('city');                                        //读取缓存城市数据

        /**
         * ===========================
         * 查询item
         * ===========================
         */
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemName',
            //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
            //'sortorder' => 'ASC'
            'sortorder' => 'ASC'
            //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        );

	
        //查询促销渠道
        $item_array['itemCategoryID'] = 1;
        $promotion_channel_array = $itemHandler->searchItem( $item_array );

       //add by 20141023 allen.qiang  lv5会员新加提示信息 ---start
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例活动类
        $activityspike=new activitySpike();
        $sql_lv5="SELECT COUNT(1) AS item_count FROM cust_master WHERE pcustID =? AND rankID=5"; //判断是否是lv5 会员 查询语句
        $sql_lv5_count="SELECT COUNT(1) AS item_count FROM bill_master WHERE pcustID =?  AND bill_status <> 10 AND billPromotion = ?  AND createTime>? AND createTime<?"; //判断是否已使用LV5五折购特权 查询语句
        //参数类型字符串
        $params_type2 = '';
        //参数值数组
        $params_array2 = array();
        $params_type2 .= 's';
        $params_array2[] =$pcustID;

        $temp_item_array = &$G_PGLOB->DBCA->stmt_query($sql_lv5,$params_type2,$params_array2);
        if($temp_item_array){
            $item_count = $temp_item_array[0]->item_count;
            if($item_count>0){ //是lv5 会员
                $params_type2 .= 's';
                $params_array2[] = $activityspike->billPromotion_lv5; //LV5会员五折购销渠道ID
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-01-01 00:00:00";
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-12-31 23:59:59";
                $temp_item_array2 = &$G_PGLOB->DBCA->stmt_query($sql_lv5_count,$params_type2,$params_array2);
                if($temp_item_array2){
                    $item_count2 = $temp_item_array2[0]->item_count;
                    if ($item_count2 > 0) { //使用过LV5五折购特权不显示LV5五折购促销
                        $item_array_lv5=array();
                        foreach ($promotion_channel_array as &$item) {
                            if($item["itemID"]!= $activityspike->billPromotion_lv5){
                                $item_array_lv5[] = array(
                                    'itemID' => $item["itemID"],
                                    'itemName' => $item["itemName"],
                                    'itemCategoryID' => $item["itemCategoryID"],
                                    'useFlg' => $item["useFlg"],
                                    'extend_1' => $item["extend_1"],
                                    'extend_2' => $item["extend_2"],
                                    'extend_3' =>$item["extend_3"],
                                    'extend_4' =>$item["extend_4"],
                                    'extend_5' => $item["extend_5"],
                                    'extend_6' => $item["extend_6"],
                                    'extend_7' => $item["extend_7"],
                                    'extend_8' =>$item["extend_8"]
                                );
                            }
                        }
                        $promotion_channel_array=$item_array_lv5;
                    }
                }
            }else{ //不是LV5会员不显示LV5五折购促销
                $item_array_lv5=array();
                foreach ($promotion_channel_array as &$item) {
                    if($item["itemID"]!= $activityspike->billPromotion_lv5){
                        $item_array_lv5[] = array(
                            'itemID' => $item["itemID"],
                            'itemName' => $item["itemName"],
                            'itemCategoryID' => $item["itemCategoryID"],
                            'useFlg' => $item["useFlg"],
                            'extend_1' => $item["extend_1"],
                            'extend_2' => $item["extend_2"],
                            'extend_3' =>$item["extend_3"],
                            'extend_4' =>$item["extend_4"],
                            'extend_5' => $item["extend_5"],
                            'extend_6' => $item["extend_6"],
                            'extend_7' => $item["extend_7"],
                            'extend_8' =>$item["extend_8"]
                        );
                    }
                }
                $promotion_channel_array=$item_array_lv5;
            }
        }
        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---end

        //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );
        //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        //配送日期
        $send_date = date('Y-m-d');

        //配送开始时间
        $item_array['itemCategoryID'] = 9;
        $send_start_time = $itemHandler->searchItem( $item_array );

        //配送结束时间
        $item_array['itemCategoryID'] = 10;
        $send_end_time = $itemHandler->searchItem( $item_array );

        //查询发票内容选项
        $item_array['itemCategoryID'] = 12;
        $invoice_content_array = $itemHandler->searchItem( $item_array );
        /**
         * ===========================
         * 查询item
         * ===========================
         */


        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */
        $checkout_array = array(
            'bill_no' => null,
            'bill_status' => 0,
            'goods_index' => 1,
            'alter_freight' => '',
            'change_time' => time(),
            'message' => null,
            'goods' => array(),
            'master' => array(
                'send_date' => $send_date,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                'billPlatform' => 1,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
                'cityid' => $cityid,
                'payTypeID' => 0,
                'payment_method_id' => 0,
                'billPromotion' => 0,
                'freight' => 0,
                'alter_freight' => 0,
                'invoice' => 0,
                'invoiceID' => 0,
                'invoice_type' => null,
                'invoice_title_type' => null,
                'invoice_title' => null,
                'distribution_id' => 0,
                'distribution_name' => 0
            )
        );

        //如果是大客户试吃订单
        if (isset($_REQUEST['bill_type']) && is_numeric($_REQUEST['bill_type'])) {
            $billType = $_REQUEST['bill_type'];
        }

        //为试吃订单时
        if ($billType == 9) {
            $checkout_array['master']['foretaste'] = 1;
        }else{
            unset($checkout_array['master']['foretaste']);
        }

        $code = strtoupper(uniqid() . rand( 100, 999 ));

        $G_PGLOB->sessionCache_set('checkout_backend_' . $code, $checkout_array, 43200, true);

        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */


        //会员优惠券
        $coupon_array = $quanliCoupon->searchCouponCode( $args_array );

        //查询大客户企业人数

        $companysql='select employees_number from company_master where pcustID=?';
        $companyNum = &$G_PGLOB->DBCA->stmt_query($companysql,'s',  $customer_array['pcustID']);
        $G_PGLOB->smarty->assign_by_ref('companyNum', $companyNum[0]->employees_number);

        $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);
        $G_PGLOB->smarty->assign_by_ref('coupon_array', $coupon_array);
        $G_PGLOB->smarty->assign_by_ref('send_date', $send_date);
        $G_PGLOB->smarty->assign_by_ref('send_start_time', $send_start_time);
        $G_PGLOB->smarty->assign_by_ref('send_end_time', $send_end_time);
        $G_PGLOB->smarty->assign_by_ref('cityid', $cityid);
        $G_PGLOB->smarty->assign_by_ref('billType', $billType);
        $G_PGLOB->smarty->assign_by_ref('city_array', $city_array);
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array', $promotion_channel_array);
        $G_PGLOB->smarty->assign_by_ref('invoice_content_array', $invoice_content_array);
        $G_PGLOB->smarty->assign_by_ref('buy_log_array', $buy_log_array);

        $G_PGLOB->smarty->assign('source', 'backend'); // 订单渠道
        $G_PGLOB->smarty->assign('code', $code); // 客服授权码
        $G_PGLOB->smarty->assign('properties', $properties); // 属性

        //add by JunHui.Li FUN-BK-0050 模糊快捷查询促销渠道功能 start
        foreach($promotion_channel_array as $k => $v){
            $buy_arrays[]=array('text'=>$v['itemName'],'id'=>$v['itemID']);

        }
        $G_PGLOB->smarty->assign_by_ref('buy_arrays', json_encode($buy_arrays));
        //add by JunHui.Li FUN-BK-0050 模糊快捷查询促销渠道功能 end
        $G_PGLOB->smarty->display('bill/billAdd.tpl');

    }

    /**
     * 新增澳粒订单
     * @author create by allen.qiang
     */
    private function AL_add(){

        if (!isset($_REQUEST['pcustID']) || empty($_REQUEST['pcustID'])){
            backendError();
        }

        //城市切换
        if(isset($_REQUEST['cityid']) && is_numeric($_REQUEST['cityid'])){
            $cityid = $_REQUEST['cityid'];
        }

        //属性
        if (!isset($_REQUEST['properties'])) {
            $properties = '';
        } else {
            $properties = $_REQUEST['properties'];
        }



        $billType = 1;  //默认正常订单

        $pcustID = $_REQUEST['pcustID'];

        global $G_PGLOB;

        //加载类文件
        quanli_require('module/base/customer/quanliCust.class.php');
        quanli_require('module/base/system/itemHandler.class.php');
        quanli_require('module/base/promotion/quanliCoupon.class.php');
        quanli_require('module/func/eai.func.php');

        //实例item类
        $itemHandler = new itemHandler();

        //实例会员类
        $quanliCust = new quanliCust();

        //实例化优惠券类
        $quanliCoupon = new quanliCoupon();

        //实例化接口操作类
        $quanliEai = eai_crm();

        //查询会员信息
        $args_array = array(
            'pcustID' => $pcustID
        );
        $customer_array = $quanliCust->searchCustomerDetail( $args_array );
        if (!$customer_array) {
            backendError('会员信息没有找到');
        }

        //会员余额
        $result_array = $quanliEai->searchCustAccount($pcustID, G_CASH_ID);
        if($result_array['code'] == 200){
            $customer_array['cash'] = $result_array['data']['useableAccount'];
        } else {
            $customer_array['cash'] = 0;
        }

        $buy_log_array = $quanliCust->getBuyLog( $pcustID );
        $buy_log_array['invoiceID'] = null;
        $buy_log_array['payTypeID'] = null;

        //查询城市信息
        $city_array = get_city();

        /**
         * ===========================
         * 查询item
         * ===========================
         */
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemName',
            //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
            //'sortorder' => 'ASC'
            'sortorder' => 'ASC'
            //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        );


        //查询促销渠道
        $item_array['itemCategoryID'] = 1;
        $promotion_channel_array = $itemHandler->searchItem( $item_array );

        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---start
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例活动类
        $activityspike=new activitySpike();
        $sql_lv5="SELECT COUNT(1) AS item_count FROM cust_master WHERE pcustID =? AND rankID=5"; //判断是否是lv5 会员 查询语句
        $sql_lv5_count="SELECT COUNT(1) AS item_count FROM bill_master WHERE pcustID =?  AND bill_status <> 10 AND billPromotion = ?  AND createTime>? AND createTime<?"; //判断是否已使用LV5五折购特权 查询语句
        //参数类型字符串
        $params_type2 = '';
        //参数值数组
        $params_array2 = array();
        $params_type2 .= 's';
        $params_array2[] =$pcustID;

        $temp_item_array = &$G_PGLOB->DBCA->stmt_query($sql_lv5,$params_type2,$params_array2);
        if($temp_item_array){
            $item_count = $temp_item_array[0]->item_count;
            if($item_count>0){ //是lv5 会员
                $params_type2 .= 's';
                $params_array2[] = $activityspike->billPromotion_lv5; //LV5会员五折购销渠道ID
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-01-01 00:00:00";
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-12-31 23:59:59";
                $temp_item_array2 = &$G_PGLOB->DBCA->stmt_query($sql_lv5_count,$params_type2,$params_array2);
                if($temp_item_array2){
                    $item_count2 = $temp_item_array2[0]->item_count;
                    if ($item_count2 > 0) { //使用过LV5五折购特权不显示LV5五折购促销
                        $item_array_lv5=array();
                        foreach ($promotion_channel_array as &$item) {
                            if($item["itemID"]!= $activityspike->billPromotion_lv5){
                                $item_array_lv5[] = array(
                                    'itemID' => $item["itemID"],
                                    'itemName' => $item["itemName"],
                                    'itemCategoryID' => $item["itemCategoryID"],
                                    'useFlg' => $item["useFlg"],
                                    'extend_1' => $item["extend_1"],
                                    'extend_2' => $item["extend_2"],
                                    'extend_3' =>$item["extend_3"],
                                    'extend_4' =>$item["extend_4"],
                                    'extend_5' => $item["extend_5"],
                                    'extend_6' => $item["extend_6"],
                                    'extend_7' => $item["extend_7"],
                                    'extend_8' =>$item["extend_8"]
                                );
                            }
                        }
                        $promotion_channel_array=$item_array_lv5;
                    }
                }
            }else{ //不是LV5会员不显示LV5五折购促销
                $item_array_lv5=array();
                foreach ($promotion_channel_array as &$item) {
                    if($item["itemID"]!= $activityspike->billPromotion_lv5){
                        $item_array_lv5[] = array(
                            'itemID' => $item["itemID"],
                            'itemName' => $item["itemName"],
                            'itemCategoryID' => $item["itemCategoryID"],
                            'useFlg' => $item["useFlg"],
                            'extend_1' => $item["extend_1"],
                            'extend_2' => $item["extend_2"],
                            'extend_3' =>$item["extend_3"],
                            'extend_4' =>$item["extend_4"],
                            'extend_5' => $item["extend_5"],
                            'extend_6' => $item["extend_6"],
                            'extend_7' => $item["extend_7"],
                            'extend_8' =>$item["extend_8"]
                        );
                    }
                }
                $promotion_channel_array=$item_array_lv5;
            }
        }
        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---end

        //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );
        //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        //配送日期
        $send_date = date('Y-m-d');

        //配送开始时间
        $item_array['itemCategoryID'] = 9;
        $send_start_time = $itemHandler->searchItem( $item_array );

        //配送结束时间
        $item_array['itemCategoryID'] = 10;
        $send_end_time = $itemHandler->searchItem( $item_array );

        //查询发票内容选项
        $item_array['itemCategoryID'] = 12;
        $invoice_content_array = $itemHandler->searchItem( $item_array );
        /**
         * ===========================
         * 查询item
         * ===========================
         */


        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */
        $checkout_array = array(
            'bill_no' => null,
            'bill_status' => 0,
            'goods_index' => 1,
            'alter_freight' => '',
            'change_time' => time(),
            'message' => null,
            'goods' => array(),
            'master' => array(
                'send_date' => $send_date,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                'billPlatform' => 2,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
                'cityid' => $cityid,
                'payTypeID' => 0,
                'payment_method_id' => 0,
                'billPromotion' => 0,
                'freight' => 0,
                'alter_freight' => 0,
                'invoice' => 0,
                'invoiceID' => 0,
                'invoice_type' => null,
                'invoice_title_type' => null,
                'invoice_title' => null,
                'distribution_id' => 0,
                'distribution_name' => 0
            )
        );

        //如果是大客户试吃订单
        if (isset($_REQUEST['bill_type']) && is_numeric($_REQUEST['bill_type'])) {
            $billType = $_REQUEST['bill_type'];
        }

        //为试吃订单时
        if ($billType == 9) {
            $checkout_array['master']['foretaste'] = 1;
        }else{
            unset($checkout_array['master']['foretaste']);
        }

        $code = strtoupper(uniqid() . rand( 100, 999 ));

        $G_PGLOB->sessionCache_set('checkout_backend_' . $code, $checkout_array, 43200, true);

        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */


        //会员优惠券
        $coupon_array = $quanliCoupon->searchCouponCode( $args_array );

        //查询大客户企业人数

        $companysql='select employees_number from company_master where pcustID=?';
        $companyNum = &$G_PGLOB->DBCA->stmt_query($companysql,'s',  $customer_array['pcustID']);
        $G_PGLOB->smarty->assign_by_ref('companyNum', $companyNum[0]->employees_number);

        $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);
        $G_PGLOB->smarty->assign_by_ref('coupon_array', $coupon_array);
        $G_PGLOB->smarty->assign_by_ref('send_date', $send_date);
        $G_PGLOB->smarty->assign_by_ref('send_start_time', $send_start_time);
        $G_PGLOB->smarty->assign_by_ref('send_end_time', $send_end_time);
        $G_PGLOB->smarty->assign_by_ref('cityid', $cityid);
        $G_PGLOB->smarty->assign_by_ref('billType', $billType);
        $G_PGLOB->smarty->assign_by_ref('city_array', $city_array);
        $G_PGLOB->smarty->assign_by_ref('invoice_content_array', $invoice_content_array);
        $G_PGLOB->smarty->assign_by_ref('buy_log_array', $buy_log_array);

        $G_PGLOB->smarty->assign('source', 'backend'); // 订单渠道
        $G_PGLOB->smarty->assign('code', $code); // 客服授权码
        $G_PGLOB->smarty->assign('properties', $properties); // 属性

        $G_PGLOB->smarty->display('bill/AL_billAdd.tpl');
    }

    /**
     * 订单修改
     */
    private function alter() {

        if (!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])) {
            backendError();
        }

        global $G_PGLOB;

        //加载类文件
        quanli_require('module/base/bill/billSearch.class.php');
        quanli_require('module/base/customer/quanliCust.class.php');
        quanli_require('module/base/customer/custDeliver.class.php');
        quanli_require('module/base/goods/quanliGoods.class.php');
        quanli_require('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/system/itemHandler.class.php');
        quanli_require('module/base/promotion/quanliCoupon.class.php');
        quanli_require('module/func/eai.func.php');

        //实例订单类
        $billSearch = new billSearch();


        //实例item类
        $itemHandler = new itemHandler();


        //实例会员类
        $quanliCust = new quanliCust();


        //实例会员地址类
        $custDeliver = new custDeliver();


        //实例优惠券类
        $quanliCoupon = new quanliCoupon();


        //实例商品类
        $quanliGoods = new quanliGoods( $G_PGLOB->DBCA );


        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_PGLOB->DBCA );


        //实例化接口操作类
        $quanliEai = eai_crm();


        //查询订单信息
        $args_array = array(
            'bill_no' => $_REQUEST['bill_no']
        );
        $bill_array = $billSearch->searchBillInfo( $args_array );
        if (!$bill_array) {
            backendError('订单信息没有找到');
        }
        if (in_array($bill_array[0]['bill_status'], array(2, 3))) {
            backendError('订单已生产，无法修改');
        }

        //mod by 20141111 sunqiang FUN-BK-0021 订单为已下车状态可修改订单 start

        if($G_PGLOB->has_permission(1029) && in_array($bill_array[0]['bill_status'], array(5, 6)))
        {
            //有权限下车状态可修改
        }
        else if(in_array($bill_array[0]['bill_status'], array(4, 5, 6)))
        {
            backendError('订单正在配送，无法修改');
        }


//        if (in_array($bill_array[0]['bill_status'], array(4, 5, 6))) {
//            backendError('订单正在配送，无法修改');
//        }
        //mod by 20141111 sunqiang FUN-BK-0021 订单为已下车状态可修改订单 end
        if ($bill_array[0]['bill_status'] == 7) {
            backendError('订单已完成，无法修改');
        }
        if ($bill_array[0]['bill_status'] == 10) {
            backendError('订单已取消，无法修改');
        }
        $bill_array = $bill_array[0];
        $pcustID = $bill_array['pcustID'];


        $billType = $bill_array['billType'];



        //订单已确认 无权限者不能修改
        if($bill_array['bill_status']==1 && $billType==9 ){
            if(!$G_PGLOB->has_permission(60004)){
                backendError('订单已被审核不能修改');
            }
        }


        /**
         * ==================
         * 检查当前订单使用的收货地址是否和现在的收货地址相同，如果不同，则新增收货地址
         * ==================
         */

        //判断配送类型 add by allen.qiang 2015-06-29
        switch( $bill_array['sendTypeID']){

            //Mcake直送
            case 6:

                $deliver_args_array = array(
                    'pcustID' => $pcustID,
                    'provinceid' => $bill_array['deliver_provinceid'],
                    'cityid' => $bill_array['deliver_cityid'],
                    'countyid' => $bill_array['deliver_countyid'],
                    'precise_address' => $bill_array['deliver_address'],
                    'custName' => $bill_array['deliver_name'],
                    'mobile' => $bill_array['deliver_mobile'],
                    'phone' => $bill_array['deliver_phone']
                );

                $deliver_array = $custDeliver->searchCustDeliver( $deliver_args_array );

                if ($deliver_array) {

                    $bill_array['deliverid'] = $deliver_array[0]['deliverID'];

                } else {

                    $deliver_args_array['creator'] = 'system';
                    $deliver_args_array['address'] = $bill_array['deliver_address'];
                    $result_array = $custDeliver->editCustDeliver( $deliver_args_array );
                    if ($result_array['code'] != 200) {
                        backendError('新增收货地址：'. $result_array['message']);
                    }
                    $bill_array['deliverid'] = $result_array['data']['deliverID'];

                }

                break;

            //站点自提
            case 8:

                $deliver_args_array = array(
                    'pcustID' => $pcustID,
                    'custName' => $bill_array['deliver_name'],
                    'mobile' => $bill_array['deliver_mobile'],
                    'phone' => $bill_array['deliver_phone'],
                    'type' => 2
                );

                $deliver_array = $custDeliver->searchCustDeliver( $deliver_args_array );

                if ($deliver_array) {

                    $bill_array['deliverid'] = $deliver_array[0]['deliverID'];

                } else {

                    $deliver_args_array = array(
                        'creator' => 'system',
                        'pcustID' => $pcustID,
                        'provinceid' => $bill_array['deliver_provinceid'],
                        'cityid' => $bill_array['deliver_cityid'],
                        'countyid' => $bill_array['deliver_countyid'],
                        'address' => $bill_array['deliver_address'],
                        'custName' => $bill_array['deliver_name'],
                        'mobile' => $bill_array['deliver_mobile'],
                        'phone' => $bill_array['deliver_phone'],
                        'type' => 2,
                        'distribution_id' => $bill_array['distribution_id']
                    );

                    $result_array = $custDeliver->editCustDeliver( $deliver_args_array );

                    if ($result_array['code'] != 200) {
                        backendError('新增收货地址：'. $result_array['message']);
                    }

                    $bill_array['deliverid'] = $result_array['data']['deliverID'];

                }

                break;
        }


        //查询当前订单是否已被代付
        $bill_paying_agent_array = $billSearch->searchBillPayingAgent( array( 'bill_no' => $_REQUEST['bill_no'] ) );
        if ($bill_paying_agent_array) {
            backendError('订单被其他订单代付，无法修改');
        }


        //查询订单优惠券
        $bill_coupon_array = $billSearch->searchBillUsedCoupon( $_REQUEST['bill_no'] );


        //查询订单代金卡
        $bill_card_array = $billSearch->searchBillUsedCard( $_REQUEST['bill_no'] );


        //查询代付订单
        $bill_paying_agent_array = $billSearch->searchBillPayingAgent( array( 'main_bill_no' => $_REQUEST['bill_no'] ) );


        //查询订单商品信息
        $bill_goods_array = $billSearch->searchBillGoods( $args_array );
        $cart_goods_array = array();
        $index = 1;
        foreach ($bill_goods_array as &$bill_goods) {

            //查询订单商品配件
            $args_array = array(
                'bill_goods_no' => $bill_goods['bill_goods_no']
            );
            $bill_goods_fittings_array = $billSearch->searchGoodsFittingsBill( $args_array );

            //查询商品信息
            $args_array = array(
                'postID' => $bill_goods['goods_postID']
            );
            $goods_array = $quanliGoods->searchGoodsSaleDetail($args_array);

            //查询商品配件
            $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $args_array );
            if ($goods_fittings_array) {
                foreach($goods_fittings_array as &$goods_fittings) {

                    if(!isset($bill_goods_fittings_array[$goods_fittings['fitID']]))
                    {
                        $goods_fittings['isdelete'] = 1;
                    }

                    if ($goods_fittings['forsale'] == 0) {
                        continue;
                    } else if (!isset($bill_goods_fittings_array[$goods_fittings['fitID']])) {
                        continue;
                    }
                    $goods_fittings['buyNum'] = $bill_goods_fittings_array[$goods_fittings['fitID']]['buyNum'];
                    //add by yim.hu FUN-BK-0009 获得单价 start
                    $goods_fittings['buyMoney'] = $bill_goods_fittings_array[$goods_fittings['fitID']]['buyMoney'];                    
                    //add by yim.hu FUN-BK-0009 获得单价 end
                }
            }

            $cart_goods_array[$index] = array (
                'bill_goods_no' => $bill_goods['bill_goods_no'],
                'goodsID' => $goods_array['goodsID'],
                'postID' => $goods_array['postID'],
                'cityID' => $goods_array['cityID'],
                'goodsName' => $goods_array['goodsName'],
                'spec' => $goods_array['spec'],
                'goods_amount' => 1,
                'salePrice' => $goods_array['salePrice'],
                'price' => $bill_goods['goods_purchasePrice'],
                'goods_remark' => $bill_goods['goods_remark'],
                'birthday_card' => $bill_goods['birthday_card'],
                'segmentation' => $bill_goods['segmentation'],
                'salePoints' => 0,
                'goodsWeight' => 0,
                'goods_from' => 1,
                'goods_groupID' => 0,
                'goods_status' => 1,
                'goodsMainPic' => $goods_array['goodsMainPic'],
                'bigCategoryID' => $goods_array['bigCategoryID'],
                'midCategoryID' => $goods_array['midCategoryID'],
                'smallCategoryID' => $goods_array['smallCategoryID'],
                'product_code' => $goods_array['product_code'],
                'syncopate' => explode('|', $goods_array['syncopate']),
                'birthcard' => explode('|', $goods_array['birthcard']),
                'twoPostID' => $goods_array['twoPostID'],
                'marketPrice' => $goods_array['marketPrice'],
                'startDeliver' => $goods_array['startDeliver'],
                'endDeliver' => $goods_array['endDeliver'],
                'deliverSpace' => $goods_array['deliverSpace'],
                'cutTime' => $goods_array['cutTime'],
                'goods_weight' => 0,
                'fittings' => $goods_fittings_array,
                'fittings_json' => json_encode($goods_fittings_array),
                'adjustPrice' => $bill_goods['auth_alter'] ? $bill_goods['goods_purchasePrice'] : -1,
                'promotion' => null
            );

            ++$index;

        }

        //查询会员信息
        $args_array = array(
            'pcustID' => $pcustID
        );
        $customer_array = $quanliCust->searchCustomerDetail( $args_array );
        if (!$customer_array) {
            backendError('会员信息没有找到');
        }

        //会员余额
        $result_array = $quanliEai->searchCustAccount($pcustID, G_CASH_ID);
        if ($result_array['code'] == 200) {
            $customer_array['cash'] = $result_array['data']['useableAccount'];
        } else {
            $customer_array['cash'] = 0;
        }

        //会员积分
        $result_array = $quanliEai->searchCustAccount($pcustID, G_POINTS_ID);
        if ($result_array['code'] == 200) {
            $customer_array['points'] = $result_array['data']['useableAccount'];
            $customer_array['points_price'] = round($result_array['data']['useableAccount'] / 100, 2);
        } else {
            $customer_array['points'] = 0;
            $customer_array['points_price'] = 0;
        }

        /**
         * ===========================
         * 查询item
         * ===========================
         */
        $item_array = array(
            'useFlg' => 1,
            //mod by allen.qiang 20140924 促销渠道数据按首字母排序--start
            //'sortname' => 'itemID',
            'sortname' => 'itemName',
            //mod by allen.qiang 20140924 促销渠道数据按首字母排序--end
            'sortorder' => 'ASC'
        );

        //查询促销渠道
        $item_array['itemCategoryID'] = 1;
        $promotion_channel_array = $itemHandler->searchItem( $item_array );

        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---start
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例活动类
        $activityspike=new activitySpike();
        $sql_lv5="SELECT COUNT(1) AS item_count FROM cust_master WHERE pcustID =? AND rankID=5"; //判断是否是lv5 会员 查询语句
        $sql_lv5_count="SELECT bill_no FROM bill_master WHERE pcustID =?  AND bill_status <> 10 AND billPromotion = ?  AND createTime>? AND createTime<?"; //判断是否已使用LV5五折购特权 查询语句
        //参数类型字符串
        $params_type2 = '';
        //参数值数组
        $params_array2 = array();
        $params_type2 .= 's';
        $params_array2[] =$pcustID;

        $temp_item_array = &$G_PGLOB->DBCA->stmt_query($sql_lv5,$params_type2,$params_array2);
        if($temp_item_array){
            $item_count = $temp_item_array[0]->item_count;
            if($item_count>0){ //是lv5 会员
                $params_type2 .= 's';
                $params_array2[] = $activityspike->billPromotion_lv5; //LV5会员五折购销渠道ID
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-01-01 00:00:00";
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-12-31 23:59:59";
                $temp_item_array2 = &$G_PGLOB->DBCA->stmt_query($sql_lv5_count,$params_type2,$params_array2);
                if($temp_item_array2){
                    $item_bill_no= $temp_item_array2[0]->bill_no;
                    if ($item_bill_no !=$_REQUEST['bill_no']) { //使用过LV5五折购特权且不是当前订单不显示LV5五折购促销
                        $item_array_lv5=array();
                        foreach ($promotion_channel_array as &$item) {
                            if($item["itemID"]!=$activityspike->billPromotion_lv5){
                                $item_array_lv5[] = array(
                                    'itemID' => $item["itemID"],
                                    'itemName' => $item["itemName"],
                                    'itemCategoryID' => $item["itemCategoryID"],
                                    'useFlg' => $item["useFlg"],
                                    'extend_1' => $item["extend_1"],
                                    'extend_2' => $item["extend_2"],
                                    'extend_3' =>$item["extend_3"],
                                    'extend_4' =>$item["extend_4"],
                                    'extend_5' => $item["extend_5"],
                                    'extend_6' => $item["extend_6"],
                                    'extend_7' => $item["extend_7"],
                                    'extend_8' =>$item["extend_8"]
                                );
                            }
                        }
                        $promotion_channel_array=$item_array_lv5;
                    }
                }
            }else{ //不是LV5会员不显示LV5五折购促销
                $item_array_lv5=array();
                foreach ($promotion_channel_array as &$item) {
                    if($item["itemID"]!=$activityspike->billPromotion_lv5){
                        $item_array_lv5[] = array(
                            'itemID' => $item["itemID"],
                            'itemName' => $item["itemName"],
                            'itemCategoryID' => $item["itemCategoryID"],
                            'useFlg' => $item["useFlg"],
                            'extend_1' => $item["extend_1"],
                            'extend_2' => $item["extend_2"],
                            'extend_3' =>$item["extend_3"],
                            'extend_4' =>$item["extend_4"],
                            'extend_5' => $item["extend_5"],
                            'extend_6' => $item["extend_6"],
                            'extend_7' => $item["extend_7"],
                            'extend_8' =>$item["extend_8"]
                        );
                    }
                }
                $promotion_channel_array=$item_array_lv5;
            }
        }
        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---end

        //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );
        //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        //配送开始时间
        $item_array['itemCategoryID'] = 9;
        $send_start_time = $itemHandler->searchItem( $item_array );

        //配送结束时间
        $item_array['itemCategoryID'] = 10;
        $send_end_time = $itemHandler->searchItem( $item_array );

        //查询发票内容选项
        $item_array['itemCategoryID'] = 12;
        $invoice_content_array = $itemHandler->searchItem( $item_array );
        /**
         * ===========================
         * 查询item
         * ===========================
         */


        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */
        $checkout_array = array(

            'bill_no' => $bill_array['bill_no'],
            'bill_status' => $bill_array['bill_status'],
            'goods_index' => $index,
            'alter_freight' => $bill_array['auth_alter'] ? $bill_array['deliver_feePaid'] : '',
            'change_time' => time(),
            'message' => null,
            'goods' => array(
                'general' => $cart_goods_array
            ),
            'master' => array(
                'send_date' => $bill_array['send_date'],
                'send_start_time' => $bill_array['send_start_time'],
                'send_end_time' => $bill_array['send_end_time'],
                'cityid' => $bill_array['deliver_cityid'],
                'payTypeID' => $bill_array['payTypeID'],
                'payment_method_id' => $bill_array['payment_method_id'],
                'billPromotion' => $bill_array['billPromotion'],
                'freight' => 0,
                'alter_freight' => 0,
                'invoice' => 0,
                'invoiceID' => 0,
                'invoice_type' => null,
                'invoice_title_type' => null,
                'invoice_title' => null,
                'distribution_id' => 0,
                'distribution_name' => 0,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                'billPlatform' => $bill_array['billPlatform']
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
            ),
            'use_coupon' => $bill_coupon_array,
            'bill_paying_agent' => $bill_paying_agent_array

        );
        if ($bill_array['bill_points_money'] > 0) {
            $checkout_array['use_points'] = array(
                'points' => $bill_array['bill_points_money'] * 100,
                'bill_points_money' => $bill_array['bill_points_money']
            );
        }
        if ($bill_array['bill_balance_money'] > 0) {
            $checkout_array['use_balance'] = array(
                'balance' => $bill_array['bill_balance_money']
            );
        }

        //订单类型：试吃订单
        if($billType == 9){
            $checkout_array['master']['foretaste'] = 1;
        }else{
            unset($checkout_array['master']['foretaste']);
        }

        $code = strtoupper(uniqid() . rand( 100, 999 ));

        $G_PGLOB->sessionCache_set('checkout_backend_' . $code, $checkout_array, 43200, true);

        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */


        //会员优惠券
        $coupon_array = $quanliCoupon->searchCouponCode( $args_array );


        //查询在线支付已付款
        $online_pay_array = $billSearch->searchBillPayDetail( array( 'bill_no' => $_REQUEST['bill_no'], 'type' => 2 ) );
        if ($online_pay_array) {
            $bill_array['bill_online_money'] = $online_pay_array['deduct_amount'];
        }

        //订单渠道
        if ($bill_array['billChannel'] == 1) {
            $source = 'web';
        } else if ($bill_array['billChannel'] == 2) {
            $source = 'wap';
        } else if ($bill_array['billChannel'] == 3) {
            $source = 'weixin';
        } else if ($bill_array['billChannel'] == 4) {
            $source = 'app';
        } else if ($bill_array['billChannel'] == 5) {
            $source = 'offline';
        } else if ($bill_array['billChannel'] == 6) {
            $source = 'backend';
        }

        $G_PGLOB->smarty->assign_by_ref('bill_array', $bill_array);
        $G_PGLOB->smarty->assign_by_ref('billType', $billType);
        $G_PGLOB->smarty->assign_by_ref('bill_goods_array', $cart_goods_array);
        $G_PGLOB->smarty->assign_by_ref('bill_coupon_array', $bill_coupon_array);
        $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);
        $G_PGLOB->smarty->assign_by_ref('coupon_array', $coupon_array);
        $G_PGLOB->smarty->assign_by_ref('bill_card_array', $bill_card_array);
        if (isset($checkout_array['bill_paying_agent'])) {
            $G_PGLOB->smarty->assign_by_ref('bill_paying_agent_array', $bill_paying_agent_array);
        }
        $G_PGLOB->smarty->assign_by_ref('send_date', $send_date);
        $G_PGLOB->smarty->assign_by_ref('send_start_time', $send_start_time);
        $G_PGLOB->smarty->assign_by_ref('send_end_time', $send_end_time);
        $G_PGLOB->smarty->assign_by_ref('city_array', $G_PGLOB->getCache('city'));
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array', $promotion_channel_array);
        $G_PGLOB->smarty->assign_by_ref('invoice_content_array', $invoice_content_array);
        $G_PGLOB->smarty->assign('cityid', $bill_array['deliver_cityid']);
        $G_PGLOB->smarty->assign('source', $source);
        $G_PGLOB->smarty->assign('code', $code);
        $G_PGLOB->smarty->assign('orders_code', $code);

        $companysql='select employees_number from company_master where pcustID=?';
        $companyNum = &$G_PGLOB->DBCA->stmt_query($companysql,'s',  $customer_array['pcustID']);
        $G_PGLOB->smarty->assign_by_ref('companyNum', $companyNum[0]->employees_number);

        if ($bill_array['auth_alter']) {
            $G_PGLOB->sessionCache_set('AUTH_' . $code, 1, 3600,true);
        }
        //add by JunHui.Li FUN-BK-0050 模糊快捷查询促销渠道功能 start
        foreach($promotion_channel_array as $k => $v){
            $buy_arrays[]=array('text'=>$v['itemName'],'id'=>$v['itemID']);

        }
        $G_PGLOB->smarty->assign_by_ref('buy_arrays', json_encode($buy_arrays));
        //add by JunHui.Li FUN-BK-0050 模糊快捷查询促销渠道功能 end
        $G_PGLOB->smarty->display('bill/billAlter.tpl');

    }

    /**
     * 修改澳粒订单
     * @author create by allen.qiang
     */
    private function AL_alter(){

        if (!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])) {
            backendError();
        }

        global $G_PGLOB;

        //加载类文件
        quanli_require('module/base/bill/billSearch.class.php');
        quanli_require('module/base/customer/quanliCust.class.php');
        quanli_require('module/base/customer/custDeliver.class.php');
        quanli_require('module/base/goods/quanliGoods.class.php');
        quanli_require('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/system/itemHandler.class.php');
        quanli_require('module/base/promotion/quanliCoupon.class.php');
        quanli_require('module/func/eai.func.php');

        //实例订单类
        $billSearch = new billSearch();


        //实例item类
        $itemHandler = new itemHandler();


        //实例会员类
        $quanliCust = new quanliCust();


        //实例会员地址类
        $custDeliver = new custDeliver();


        //实例优惠券类
        $quanliCoupon = new quanliCoupon();


        //实例商品类
        $quanliGoods = new quanliGoods( $G_PGLOB->DBCA );


        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_PGLOB->DBCA );


        //实例化接口操作类
        $quanliEai = eai_crm();


        //查询订单信息
        $args_array = array(
            'bill_no' => $_REQUEST['bill_no']
        );
        $bill_array = $billSearch->searchBillInfo( $args_array );

        if (!$bill_array) {
            backendError('订单信息没有找到');
        }
        if (in_array($bill_array[0]['bill_status'], array(2, 3))) {
            backendError('订单已生产，无法修改');
        }
        //mod by 20141111 sunqiang FUN-BK-0021 订单为已下车状态可修改订单 start

        if($G_PGLOB->has_permission(1029) && in_array($bill_array[0]['bill_status'], array(5, 6)))
        {
            //有权限下车状态可修改
        }
        else if(in_array($bill_array[0]['bill_status'], array(4, 5, 6)))
        {
            backendError('订单正在配送，无法修改');
        }


//        if (in_array($bill_array[0]['bill_status'], array(4, 5, 6))) {
//            backendError('订单正在配送，无法修改');
//        }
        //mod by 20141111 sunqiang FUN-BK-0021 订单为已下车状态可修改订单 end
        if ($bill_array[0]['bill_status'] == 7) {
            backendError('订单已完成，无法修改');
        }
        if ($bill_array[0]['bill_status'] == 10) {
            backendError('订单已取消，无法修改');
        }
        $bill_array = $bill_array[0];
        $pcustID = $bill_array['pcustID'];

        $billType = $bill_array['billType'];

        //订单已确认 无权限者不能修改
        if($bill_array['bill_status']==1 && $billType==9 ){
            if(!$G_PGLOB->has_permission(60004)){
                backendError('订单已被审核不能修改');
            }
        }

        /**
         * ======================
         * 检查当前订单使用的
         * 收货地址是否和现在
         * 的收货地址相同，如
         * 果不同，则新增收货地址
         * ======================
         */

        $deliver_args_array = array(
            'pcustID' => $pcustID,
            'provinceid' => $bill_array['deliver_provinceid'],
            'cityid' => $bill_array['deliver_cityid'],
            'countyid' => $bill_array['deliver_countyid'],
            'precise_address' => $bill_array['deliver_address'],
            'custName' => $bill_array['deliver_name'],
            'mobile' => $bill_array['deliver_mobile'],
            'phone' => $bill_array['deliver_phone']
        );
        $deliver_array = $custDeliver->searchCustDeliver( $deliver_args_array );
        if ($deliver_array) {

            $bill_array['deliverid'] = $deliver_array[0]['deliverID'];

        } else {

            $deliver_args_array['creator'] = 'system';
            $deliver_args_array['address'] = $bill_array['deliver_address'];
            $result_array = $custDeliver->editCustDeliver( $deliver_args_array );
            if ($result_array['code'] != 200) {
                backendError('新增收货地址：'. $result_array['message']);
            }
            $bill_array['deliverid'] = $result_array['data']['deliverID'];

        }

        //查询当前订单是否已被代付
        $bill_paying_agent_array = $billSearch->searchBillPayingAgent( array( 'bill_no' => $_REQUEST['bill_no'] ) );
        if ($bill_paying_agent_array) {
            backendError('订单被其他订单代付，无法修改');
        }

        //查询订单优惠券
        $bill_coupon_array = $billSearch->searchBillUsedCoupon( $_REQUEST['bill_no'] );

        //查询订单代金卡
        $bill_card_array = $billSearch->searchBillUsedCard( $_REQUEST['bill_no'] );

        //查询代付订单
        $bill_paying_agent_array = $billSearch->searchBillPayingAgent( array( 'main_bill_no' => $_REQUEST['bill_no'] ) );

        //查询订单商品信息
        $bill_goods_array = $billSearch->searchBillGoods( $args_array );
        $cart_goods_array = array();
        $index = 1;
        foreach ($bill_goods_array as &$bill_goods) {

            //查询订单商品配件
            $args_array = array(
                'bill_goods_no' => $bill_goods['bill_goods_no']
            );
            $bill_goods_fittings_array = $billSearch->searchGoodsFittingsBill( $args_array );

            //查询商品信息
            $args_array = array(
                'postID' => $bill_goods['goods_postID']
            );
            $goods_array = $quanliGoods->searchGoodsSaleDetail($args_array);

            //查询商品配件
            $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $args_array );
            if ($goods_fittings_array) {
                foreach($goods_fittings_array as &$goods_fittings) {
                    if ($goods_fittings['forsale'] == 0) {
                        continue;
                    } else if (!isset($bill_goods_fittings_array[$goods_fittings['fitID']])) {
                        continue;
                    }
                    $goods_fittings['buyNum'] = $bill_goods_fittings_array[$goods_fittings['fitID']]['buyNum'];
                    //add by yim.hu FUN-BK-0009 获得单价 start
                    $goods_fittings['buyMoney'] = $bill_goods_fittings_array[$goods_fittings['fitID']]['buyMoney'];
                    //add by yim.hu FUN-BK-0009 获得单价 end
                }
            }

            $cart_goods_array[$index] = array (
                'bill_goods_no' => $bill_goods['bill_goods_no'],
                'goodsID' => $goods_array['goodsID'],
                'postID' => $goods_array['postID'],
                'goodsName' => $goods_array['goodsName'],
                'spec' => $goods_array['spec'],
                'goods_amount' => 1,
                'salePrice' => $goods_array['salePrice'],
                'price' => $bill_goods['goods_purchasePrice'],
                'goods_remark' => $bill_goods['goods_remark'],
                'birthday_card' => $bill_goods['birthday_card'],
                'segmentation' => $bill_goods['segmentation'],
                'salePoints' => 0,
                'goodsWeight' => 0,
                'goods_from' => 1,
                'goods_groupID' => 0,
                'goods_status' => 1,
                'goodsMainPic' => $goods_array['goodsMainPic'],
                'bigCategoryID' => $goods_array['bigCategoryID'],
                'midCategoryID' => $goods_array['midCategoryID'],
                'smallCategoryID' => $goods_array['smallCategoryID'],
                'product_code' => $goods_array['product_code'],
                'syncopate' => explode('|', $goods_array['syncopate']),
                'birthcard' => explode('|', $goods_array['birthcard']),
                'twoPostID' => $goods_array['twoPostID'],
                'marketPrice' => $goods_array['marketPrice'],
                'startDeliver' => $goods_array['startDeliver'],
                'endDeliver' => $goods_array['endDeliver'],
                'deliverSpace' => $goods_array['deliverSpace'],
                'cutTime' => $goods_array['cutTime'],
                'goods_weight' => 0,
                'fittings' => $goods_fittings_array,
                'fittings_json' => json_encode($goods_fittings_array),
                'adjustPrice' => $bill_goods['auth_alter'] ? $bill_goods['goods_purchasePrice'] : -1,
                'promotion' => null
            );

            ++$index;

        }

        //查询会员信息
        $args_array = array(
            'pcustID' => $pcustID
        );
        $customer_array = $quanliCust->searchCustomerDetail( $args_array );
        if (!$customer_array) {
            backendError('会员信息没有找到');
        }

        //会员余额
        $result_array = $quanliEai->searchCustAccount($pcustID, G_CASH_ID);
        if ($result_array['code'] == 200) {
            $customer_array['cash'] = $result_array['data']['useableAccount'];
        } else {
            $customer_array['cash'] = 0;
        }

        //会员积分
        $result_array = $quanliEai->searchCustAccount($pcustID, G_POINTS_ID);
        if ($result_array['code'] == 200) {
            $customer_array['points'] = $result_array['data']['useableAccount'];
            $customer_array['points_price'] = round($result_array['data']['useableAccount'] / 100, 2);
        } else {
            $customer_array['points'] = 0;
            $customer_array['points_price'] = 0;
        }

        /**
         * ===========================
         * 查询item
         * ===========================
         */
        $item_array = array(
            'useFlg' => 1,
            //mod by allen.qiang 20140924 促销渠道数据按首字母排序--start
            //'sortname' => 'itemID',
            'sortname' => 'itemName',
            //mod by allen.qiang 20140924 促销渠道数据按首字母排序--end
            'sortorder' => 'ASC'
        );

        //查询促销渠道
        $item_array['itemCategoryID'] = 1;
        $promotion_channel_array = $itemHandler->searchItem( $item_array );

        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---start
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例活动类
        $activityspike=new activitySpike();
        $sql_lv5="SELECT COUNT(1) AS item_count FROM cust_master WHERE pcustID =? AND rankID=5"; //判断是否是lv5 会员 查询语句
        $sql_lv5_count="SELECT bill_no FROM bill_master WHERE pcustID =?  AND bill_status <> 10 AND billPromotion = ?  AND createTime>? AND createTime<?"; //判断是否已使用LV5五折购特权 查询语句
        //参数类型字符串
        $params_type2 = '';
        //参数值数组
        $params_array2 = array();
        $params_type2 .= 's';
        $params_array2[] =$pcustID;

        $temp_item_array = &$G_PGLOB->DBCA->stmt_query($sql_lv5,$params_type2,$params_array2);
        if($temp_item_array){
            $item_count = $temp_item_array[0]->item_count;
            if($item_count>0){ //是lv5 会员
                $params_type2 .= 's';
                $params_array2[] = $activityspike->billPromotion_lv5; //LV5会员五折购销渠道ID
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-01-01 00:00:00";
                $params_type2 .= 's';
                $params_array2[] =date('Y')."-12-31 23:59:59";
                $temp_item_array2 = &$G_PGLOB->DBCA->stmt_query($sql_lv5_count,$params_type2,$params_array2);
                if($temp_item_array2){
                    $item_bill_no= $temp_item_array2[0]->bill_no;
                    if ($item_bill_no !=$_REQUEST['bill_no']) { //使用过LV5五折购特权且不是当前订单不显示LV5五折购促销
                        $item_array_lv5=array();
                        foreach ($promotion_channel_array as &$item) {
                            if($item["itemID"]!=$activityspike->billPromotion_lv5){
                                $item_array_lv5[] = array(
                                    'itemID' => $item["itemID"],
                                    'itemName' => $item["itemName"],
                                    'itemCategoryID' => $item["itemCategoryID"],
                                    'useFlg' => $item["useFlg"],
                                    'extend_1' => $item["extend_1"],
                                    'extend_2' => $item["extend_2"],
                                    'extend_3' =>$item["extend_3"],
                                    'extend_4' =>$item["extend_4"],
                                    'extend_5' => $item["extend_5"],
                                    'extend_6' => $item["extend_6"],
                                    'extend_7' => $item["extend_7"],
                                    'extend_8' =>$item["extend_8"]
                                );
                            }
                        }
                        $promotion_channel_array=$item_array_lv5;
                    }
                }
            }else{ //不是LV5会员不显示LV5五折购促销
                $item_array_lv5=array();
                foreach ($promotion_channel_array as &$item) {
                    if($item["itemID"]!=$activityspike->billPromotion_lv5){
                        $item_array_lv5[] = array(
                            'itemID' => $item["itemID"],
                            'itemName' => $item["itemName"],
                            'itemCategoryID' => $item["itemCategoryID"],
                            'useFlg' => $item["useFlg"],
                            'extend_1' => $item["extend_1"],
                            'extend_2' => $item["extend_2"],
                            'extend_3' =>$item["extend_3"],
                            'extend_4' =>$item["extend_4"],
                            'extend_5' => $item["extend_5"],
                            'extend_6' => $item["extend_6"],
                            'extend_7' => $item["extend_7"],
                            'extend_8' =>$item["extend_8"]
                        );
                    }
                }
                $promotion_channel_array=$item_array_lv5;
            }
        }
        //add by 20141023 allen.qiang  lv5会员新加提示信息 ---end

        //add by allen.qiang 20140924 促销渠道数据按首字母排序--start
        $item_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );
        //add by allen.qiang 20140924 促销渠道数据按首字母排序--end
        //配送开始时间
        $item_array['itemCategoryID'] = 9;
        $send_start_time = $itemHandler->searchItem( $item_array );

        //配送结束时间
        $item_array['itemCategoryID'] = 10;
        $send_end_time = $itemHandler->searchItem( $item_array );

        //查询发票内容选项
        $item_array['itemCategoryID'] = 12;
        $invoice_content_array = $itemHandler->searchItem( $item_array );
        /**
         * ===========================
         * 查询item
         * ===========================
         */


        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */
        $checkout_array = array(

            'bill_no' => $bill_array['bill_no'],
            'bill_status' => 0,
            'goods_index' => $index,
            'alter_freight' => $bill_array['auth_alter'] ? $bill_array['deliver_feePaid'] : '',
            'change_time' => time(),
            'message' => null,
            'goods' => array(
                'general' => $cart_goods_array
            ),
            'master' => array(
                'send_date' => $bill_array['send_date'],
                'cityid' => $bill_array['deliver_cityid'],
                'payTypeID' => $bill_array['payTypeID'],
                'payment_method_id' => $bill_array['payment_method_id'],
                'billPromotion' => $bill_array['billPromotion'],
                'freight' => 0,
                'alter_freight' => 0,
                'invoice' => 0,
                'invoiceID' => 0,
                'invoice_type' => null,
                'invoice_title_type' => null,
                'invoice_title' => null,
                'distribution_id' => 0,
                'distribution_name' => 0,
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
                'billPlatform' => $bill_array['billPlatform']
		//add 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
            ),
            'use_coupon' => $bill_coupon_array,
            'bill_paying_agent' => $bill_paying_agent_array

        );
        if ($bill_array['bill_points_money'] > 0) {
            $checkout_array['use_points'] = array(
                'points' => $bill_array['bill_points_money'] * 100,
                'bill_points_money' => $bill_array['bill_points_money']
            );
        }
        if ($bill_array['bill_balance_money'] > 0) {
            $checkout_array['use_balance'] = array(
                'balance' => $bill_array['bill_balance_money']
            );
        }

        //订单类型：试吃订单
        if($billType == 9){
            $checkout_array['master']['foretaste'] = 1;
        }else{
            unset($checkout_array['master']['foretaste']);
        }

        $code = strtoupper(uniqid() . rand( 100, 999 ));

        $G_PGLOB->sessionCache_set('checkout_backend_' . $code, $checkout_array, 43200, true);

        /**
         * ============================
         * 生成订单结算数据
         * ============================
         */


        //会员优惠券
        $coupon_array = $quanliCoupon->searchCouponCode( $args_array );


        //查询在线支付已付款
        $online_pay_array = $billSearch->searchBillPayDetail( array( 'bill_no' => $_REQUEST['bill_no'], 'type' => 2 ) );
        if ($online_pay_array) {
            $bill_array['bill_online_money'] = $online_pay_array['deduct_amount'];
        }

        //订单渠道
        if ($bill_array['billChannel'] == 1) {
            $source = 'web';
        } else if ($bill_array['billChannel'] == 2) {
            $source = 'wap';
        } else if ($bill_array['billChannel'] == 3) {
            $source = 'weixin';
        } else if ($bill_array['billChannel'] == 4) {
            $source = 'app';
        } else if ($bill_array['billChannel'] == 5) {
            $source = 'offline';
        } else if ($bill_array['billChannel'] == 6) {
            $source = 'backend';
        }

        $G_PGLOB->smarty->assign_by_ref('bill_array', $bill_array);
        $G_PGLOB->smarty->assign_by_ref('billType', $billType);
        $G_PGLOB->smarty->assign_by_ref('bill_goods_array', $cart_goods_array);
        $G_PGLOB->smarty->assign_by_ref('bill_coupon_array', $bill_coupon_array);
        $G_PGLOB->smarty->assign_by_ref('customer_array', $customer_array);
        $G_PGLOB->smarty->assign_by_ref('coupon_array', $coupon_array);
        $G_PGLOB->smarty->assign_by_ref('bill_card_array', $bill_card_array);
        if (isset($checkout_array['bill_paying_agent'])) {
            $G_PGLOB->smarty->assign_by_ref('bill_paying_agent_array', $bill_paying_agent_array);
        }
        $G_PGLOB->smarty->assign_by_ref('send_date', $send_date);
        $G_PGLOB->smarty->assign_by_ref('send_start_time', $send_start_time);
        $G_PGLOB->smarty->assign_by_ref('send_end_time', $send_end_time);
        $G_PGLOB->smarty->assign_by_ref('city_array', $G_PGLOB->getCache('city'));
        $G_PGLOB->smarty->assign_by_ref('promotion_channel_array', $promotion_channel_array);
        $G_PGLOB->smarty->assign_by_ref('invoice_content_array', $invoice_content_array);
        $G_PGLOB->smarty->assign('cityid', $bill_array['deliver_cityid']);
        $G_PGLOB->smarty->assign('source', $source);
        $G_PGLOB->smarty->assign('code', $code);
        $G_PGLOB->smarty->assign('orders_code', $code);

        $companysql='select employees_number from company_master where pcustID=?';
        $companyNum = &$G_PGLOB->DBCA->stmt_query($companysql,'s',  $customer_array['pcustID']);
        $G_PGLOB->smarty->assign_by_ref('companyNum', $companyNum[0]->employees_number);

        if ($bill_array['auth_alter']) {
            $G_PGLOB->sessionCache_set('AUTH_' . $code, 1, 3600,true);
        }

        $G_PGLOB->smarty->display('bill/AL_billAlter.tpl');

    }


}